package com.hctg.forced2sleep.easy_window;

import android.content.ComponentName;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.actor.myandroidframework.utils.LogUtils;
import com.actor.myandroidframework.utils.audio.AudioUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.databinding.FloatMonitorEditBinding;
import com.hctg.forced2sleep.utils.UsageStatsUtils;
import com.hjq.window.EasyWindow;
import com.hjq.window.draggable.SpringBackDraggable;

/**
 * description: 添加监督
 * company    :
 *
 * @author : ldf
 * date       : 2024/8/2 on 21
 * @version 1.0
 */
public class AddMonitorEasyWindow extends EasyWindow2<AddMonitorEasyWindow> implements EasyWindow.OnClickListener<View> {

    public final FloatMonitorEditBinding viewBinding;

    public AddMonitorEasyWindow() {
        super(ConfigUtils.APPLICATION);
        SpringBackDraggable springBackDraggable = new SpringBackDraggable(SpringBackDraggable.ORIENTATION_HORIZONTAL);
        //是否允许移动到挖孔屏区域
        springBackDraggable.setAllowMoveToScreenNotch(false);

        viewBinding = FloatMonitorEditBinding.inflate(LayoutInflater.from(getContext()), (ViewGroup) getDecorView(), false);
        setContentView(viewBinding.getRoot());
        setGravity(Gravity.START | Gravity.TOP);
        setYOffset(200);
        // 设置指定的拖拽规则
        setDraggable(springBackDraggable);
        setTag(this.getClass().getName());
        //添加监督
        setOnClickListener(viewBinding.button.getId(), this);
        //取消
        setOnClickListener(viewBinding.btnCancel.getId(), this);
    }

    @Override
    public void onClick(EasyWindow<?> easyWindow, View view) {
        switch (view.getId()) {
            case R.id.button:
                ComponentName processName = UsageStatsUtils.getProcessName();
                LogUtils.error(processName);
                viewBinding.btnCancel.setText(String.valueOf(processName));
//                if (processName == null) {
//                    viewBinding.btnCancel.setText(null);
//                } else {
//                    viewBinding.btnCancel.setText(processName.getClassName());
//                }

                //播放语音
                AudioUtils.getInstance().playRaw(ConfigUtils.APPLICATION, R.raw.page_scan, false, true, false, null);
                if (!EasyWindow.existShowingByClass(PageElementSelectEasyWindow.class)) {
                    viewBinding.btnCancel.setVisibility(View.VISIBLE);
                    //先隐藏, 一会儿再显示在最上面
                    this.cancel();

                    //有状态栏
                    new PageElementSelectEasyWindow().show();

//                    if (nodesViewHelper == null) {
//                        nodesViewHelper = new AccessibilityNodesViewHelper();
//                    }
//                    nodesViewHelper.showAccessibilityNodesView();

                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            show();
                        }
                    }, 1000L);

                }
                break;
            case R.id.btn_cancel:
//                if (nodesViewHelper != null) {
//                    nodesViewHelper.setCancel(true);
//                }
                //if选择元素3菜单存在
                if (EasyWindow.existShowingByClass(RectMenuEasyWindow.class)) {
                    EasyWindow.recycleByClass(RectMenuEasyWindow.class);
                    return;
                }
                //
                if (EasyWindow.existShowingByClass(PageElementSelectEasyWindow.class)) {
                    EasyWindow.recycleByClass(PageElementSelectEasyWindow.class);
                    //为何要.cancel(), 再.show(), 因为 ↑ 这个Window是全屏, 回来后会导致点击跳动偏移
                    this.cancel();
                    this.show();
                } else {
                    viewBinding.btnCancel.setVisibility(View.GONE);
                    this.recycle();
                }
                break;
            default:
                // 点击后跳转到拨打电话界面
                // Intent intent = new Intent(Intent.ACTION_DIAL);
                // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // toast.startActivity(intent);
                // 安卓 10 在后台跳转 Activity 需要额外适配
                // https://developer.android.google.cn/about/versions/10/privacy/changes#background-activity-starts
                break;
        }
    }
}
